__author__ = 'Totomo'
# 参考1.10的综合案例中的学生信息管理，来实现一个自动取款机的存取款模拟效果。要求有登陆和退出、查询余额、取钱，存钱等操作。

# 定义一个存储用户信息的列表
acclist = [
    {"accid":"totomo", "password":"1234", "accbalance":1000},
    {"accid":"alice", "password":"1234", "accbalance":500},
    {"accid":"ryan", "password":"1234", "accbalance":1500}]

while True:
    # 输出初始界面
    print("=" * 20, "自动取款机", "=" * 20)
    print("{0:1} {1:13} {2:15}".format(" ", "1. 登录", "2. 退出系统"))
    print("=" * 50)
    key = input("请输入对应的选择：")
    check = 0
    # 根据键盘值，判断并执行对应的操作
    if key=="1":
        id = input("请输入账号：")
        for i in range(0,len(acclist)):
            if id == acclist[i]["accid"]:
                pw = input("请输入密码：")
                if pw == acclist[i]["password"]:
                    while True:
                        print("=" * 20, id," 你好！", "=" * 20)
                        print("{0:1} {1:13} {2:15}".format(" ", "1. 查询余额", "2. 取钱"))
                        print("{0:1} {1:13} {2:15}".format(" ", "3. 存钱", "4. 退出登录"))
                        print("=" * 50)
                        key = input("请输入对应的选择：")
                        # 根据键盘值，判断并执行对应的操作
                        if key=="1":
                            print("=" * 20, id, "的余额", "=" * 20)
                            print("  ", acclist[i]["accbalance"], "  ")
                            input("按回车键继续：")
                        elif key=="2":
                            print("=" * 20, id, "的余额还有", acclist[i]["accbalance"], "=" * 20)
                            withdraw = int(input("请问您想取多少："))
                            if 0<withdraw<=acclist[i]["accbalance"]:
                                acclist[i]["accbalance"] -= withdraw
                                print("成功取出！")
                                input("按回车键继续：")
                            else:
                                print("金额错误或高于您的余额")
                                input("按回车键继续：")
                        elif key=="3":
                            print("=" * 20, id, "的余额还有", acclist[i]["accbalance"], "=" * 20)
                            save = int(input("请问您想存多少："))
                            if save>0:
                                acclist[i]["accbalance"] += save
                                print("成功存入！")
                                input("按回车键继续：")
                            else:
                                print("金额错误")
                                input("按回车键继续：")
                        elif key=="4":
                            check = 3
                            print("=" * 20, "退出成功", "=" * 20)
                            break
                        else:
                            print("========== 无效的键盘输入！ ============")
                else:
                    check==3
                    print("密码错误！请重新登录！")
            elif check==3:
                break
            elif check==2:
                print("此账号不存在！请重新登录！")
            check += 1

    elif key == "2":
        print("=" * 20, "再见", "=" * 20)
        break
    else:
        print("========== 无效的键盘输入！ ============")
